"""配置日志"""

import os
import logging
from logging.handlers import TimedRotatingFileHandler

from config.local import Config


def init_logger(app):
    # 配置 logger handle

    log_level = Config.LOG_LEVEL
    if not log_level:
        log_level = 'DEBUG'
    log_level = log_level.upper()

    BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
    os.makedirs(os.path.join(BASE_DIR, 'logs'), exist_ok=True)

    file_handler = TimedRotatingFileHandler(os.path.join('{}/logs'.format(BASE_DIR), 'B_admin.log'), 'midnight')
    file_handler.setLevel(level=log_level)
    file_handler.setFormatter(
        logging.Formatter('[%(asctime)s>] [%(levelname)s] <-%(filename)s-line %(lineno)d>  %(message)s')
    )
    app.logger.addHandler(file_handler)
    app.logger.setLevel(level=log_level)
